using System.Collections.Generic;
using System.IO;
using JsonRpcGen.TypeHandler;
using Newtonsoft.Json.Linq;

namespace JsonRpcGen.ReturnHandler
{
    internal class ArrayReturnHandler : ReturnHandler
    {
        private JProperty _method;
        private string _methodName;
        private JToken _properties;
        private string _className;
        private bool _done;

        public ArrayReturnHandler(JProperty method):base(null)
        {
            _method = method;
            var parts = method.Name.Split('.');
            _methodName = parts[1];
            _properties = _method.First["returns"]["items"]["properties"];
            Name = _methodName + "Response[]";
            _className = _methodName + "Response";
        }

        public override void WriteType(StreamWriter writer, Dictionary<string, string> replacements)
        {
            if (!_done)
            {
                var typeHandler = new ObjectTypeHandler(_properties);
                typeHandler.WriteClass(writer, _className, replacements);
                _done = true;
            }
        }
    }
}